      SUBROUTINE SOLONOII_MPI & 
      & (xdd2, &
      & tsnoi, tsurw, t2noi, &
      & wgnoi, w2noi, wrnoi, &
      & veg, z0, z0w, z0h, z0hw, &
      & xlai, rsmin, alb, zonirr, xlake, watdepth, &
      & tsmed, rra, emis, temmin, temmax, evapor, &
      & z0hz0, xice, snowini, iclay, isand, iveg, &
      & nx, ny, li, lf, fnmap)

      use MPI_VARIABLES, only : &
      & nx0i, nx1e, ny0i, ny1e, &
      & nxs2i, nxs2e, nys2i, nys2e

      implicit none

      ! Input/output variables
      real(8), intent(out) :: xdd2(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: tsnoi(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: tsurw(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: t2noi(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: wgnoi(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: w2noi(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: wrnoi(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: veg(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: z0(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: z0w(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: z0h(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: z0hw(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: xlai(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: rsmin(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: alb(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: zonirr(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: xlake(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(inout) :: watdepth(nxs2i:nxs2e,nys2i:nys2e)
      real(8), intent(out) :: tsmed(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: rra(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: emis(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: temmin(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: temmax(nxs2i:nxs2e,nys2i:nys2e) 
      real(8), intent(out) :: evapor(nxs2i:nxs2e,nys2i:nys2e)
      real(8), intent(out) :: xice(nxs2i:nxs2e,nys2i:nys2e)
      real(8), intent(out) :: snowini(nxs2i:nxs2e,nys2i:nys2e)

      real(8), intent(in) :: z0hz0 

      integer(4), intent(out) :: iclay(nxs2i:nxs2e,nys2i:nys2e) 
      integer(4), intent(out) :: isand(nxs2i:nxs2e,nys2i:nys2e) 
      integer(4), intent(out) :: iveg(nxs2i:nxs2e,nys2i:nys2e) 
      integer(4), intent(in) :: nx, ny
      integer(4), intent(out) :: li, lf

      character(len=80), intent(in) :: fnmap
      
      ! Local variables

      real(8), allocatable :: work(:,:)
      real(8) :: zplc, zle, zletr
      real(8) :: ruigc, ruitc

      integer(4), allocatable :: iwork(:,:)
      integer(4) :: ix, iy ! Loop indices

      character(len=80), external :: fext
!
!   esta rotina tem que ser reescrita para dar conta das heterogeneidades
!    da superficie!!!!!

!     open (1,file=fext(fnmap,'sol'),status='old')
!     read (1,*) xdd2i
!     read (1,*) ivegi
!     read (1,*) isoli
!     read (1,*) ts0
!     read (1,*) t20
!     read (1,*) wg0
!     read (1,*) w20
!     read (1,*) vegi
!     read (1,*) z0i
!     read (1,*) z0wi
!     read (1,*) z0hi
!     read (1,*) xlaii
!     read (1,*) rsmini
!     read (1,*) albi
!     read (1,*) emisi
!     close (1)
!----------------------------------------------------------------------

      allocate (work(0:nx+1,0:ny+1))
      allocate (iwork(0:nx+1,0:ny+1))

      open (1,file=fext(fnmap,'xdd'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      xdd2(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'ts '),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      tsnoi(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'tsw'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      tsurw(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'t2 '),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      t2noi(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'wg '),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      wgnoi(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'w2 '),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      w2noi(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'wr '),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      wrnoi(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'veg'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      veg(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'z0 '),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      z0(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'z0w'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      z0w(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'lai'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      xlai(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'rsm'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      rsmin(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'alb'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      alb(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'icl'),status='old')
      call ireadgrd(1,iwork,0,nx+1,0,ny+1)
      iclay(nx0i:nx1e,ny0i:ny1e) = iwork(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'isa'),status='old')
      call ireadgrd(1,iwork,0,nx+1,0,ny+1)
      isand(nx0i:nx1e,ny0i:ny1e) = iwork(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'ive'),status='old')
      call ireadgrd(1,iwork,0,nx+1,0,ny+1)
      iveg(nx0i:nx1e,ny0i:ny1e) = iwork(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'irr'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      zonirr(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

!     Start debug      
!      call ARRGATHERWRI_MPI &
!      & (zonirr,nxs2i,nxs2e,nys2i,nys2e,0,0, &
!      & 0,nx+1,0,ny+1,0,0,nx+1,ny+1,0,'zonirr1')
!     End debug      

      open (1,file=fext(fnmap,'lak'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      xlake(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'dep'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      watdepth(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'tsm'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      tsmed(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)
      
      open (1,file=fext(fnmap,'ice'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      xice(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)

      open (1,file=fext(fnmap,'snw'),status='old')
      call readgrd(1,work,0,nx+1,0,ny+1)
      snowini(nx0i:nx1e,ny0i:ny1e) = work(nx0i:nx1e,ny0i:ny1e)
      close(1)


      deallocate (work)
      deallocate (iwork)

      do ix = nx0i, nx1e !0, nx1
      do iy = ny0i, ny1e !0, ny1
         rra(ix,iy) = 1.
         z0h(ix,iy) = z0(ix,iy)*z0hz0
         z0hw(ix,iy) = z0w(ix,iy)*z0hz0
         if (iclay(ix,iy) < 0) then
            emis(ix,iy) = 1.
         else
            emis(ix,iy) = 0.98
         endif
         temmin(ix,iy) = tsnoi(ix,iy)
         temmax(ix,iy) = tsnoi(ix,iy)
         evapor(ix,iy) = 0.
      enddo
      enddo

      zplc = 0.
      zle = 0.
      zletr = 0.
      ruigc = 0.
      ruitc = 0.
      li = 2
      lf = 1

      
      return
      END SUBROUTINE SOLONOII_MPI
